/*
 * Copyright (c) 2010, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sk.jp.commons.ui.ExceptionDialog;
import sk.jp.commons.ui.LinePanel;
import sk.jp.commons.utils.LogMsg;
import sk.jp.jitag.ui.common.IconLoader;
import sk.jp.jitag.ui.common.Icons;
import sk.jp.jitag.util.Msg;

/**
 * Input fields of search filter.
 */
public class SearchModePanel extends JPanel implements ActionListener {

    private static final Logger log = LoggerFactory.getLogger(SearchModePanel.class);

    private enum ActionCode {
        menuExit,
        ;
    }

    private JFrame ownerWindow;

    private JTextField tagsTextbox;

    private JTextField addTagTextBox;//todo remove

    //----------------------------------------------------------------------

    public SearchModePanel(JFrame ownerWindow) {
        this.ownerWindow = ownerWindow;

        init();
    }

    //----------------------------------------------------------------------

    //----------------------------------------------------------------------

    private void init(){
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JPanel filterPanel = this; //prepared for future changes of layout

        JPanel linePanel;

        //Tags
        linePanel = new LinePanel();
        add(linePanel);
        linePanel.setLayout(new BoxLayout(linePanel, BoxLayout.X_AXIS));
        linePanel.add(new JLabel("Tags"));


        


        /////////////


        //

        linePanel = new LinePanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        add(linePanel);
        linePanel.add(new JLabel(Msg.m.value())); //Edit images

        //

        linePanel = new LinePanel();
        add(linePanel);
        linePanel.setLayout(new BoxLayout(linePanel, BoxLayout.X_AXIS));

        addTagTextBox = new JTextField("");
        linePanel.add(addTagTextBox);

        ImageIcon addIcon = IconLoader.loadIcon(Icons.add);
        JButton button = new JButton(addIcon);
        linePanel.add(button);
        button.setMargin(new Insets(1, 1, 1, 1));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("text: "+addTagTextBox.getText());//TODO test
            }
        });

//        add(Box.createVerticalGlue());

        JList allTagsList = new JList(new String[]{"fotky", "vylet", "recept"});
        add(allTagsList);
        allTagsList.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
    }

    public void actionPerformed(ActionEvent event) {
        processAction(event.getActionCommand());
    }

    /**
     * Performs specified action command.
     * @param command see {@link ActionCode}.
     * @return true if action was performed successfully, false if
     * any exception occurrs or if specified command is not supported.
     */
    private boolean processAction(String command){
        try {
            log.debug(LogMsg.inp("command", command));
/*
            if(ActionCode.menuExit.name().equals(command)){
                actionExit();
            }else if(ActionCode.menuRefreshRepNow.name().equals(command)){
                actionRefreshRepositoryNow();
            }else if(ActionCode.menuZoom.name().equals(command)){
                actionZoomImage();
            }else if(command.startsWith(ActionCode.menuOpenWithTool.name())){
                String toolLabel = command.substring(command.indexOf('$')+1);
                actionOpenWithTool(toolLabel);
            }else if(ActionCode.menuOpenProperties.name().equals(command)){
                actionOpenPreferences();
            }else if(ActionCode.menuEditMode.name().equals(command)){
                actionEditMode();
            }else if(ActionCode.menuSearchMode.name().equals(command)){
                actionSearchMode();
            }else if(ActionCode.menuSystemInfo.name().equals(command)){
                actionSystemInfo();
            }else if(ActionCode.runGarbageCollector.name().equals(command)){
                actionRunGarbageCollector();
            }else if(ActionCode.menuOpenAbout.name().equals(command)){
                actionOpenAbout();
            }else{
                log.warn(LogMsg.toString("Unknown action command", "command", command));
                return false;
            }
*/
            return true;
        } catch (Throwable e) {
            log.error("Unexpected exception", e);
            try {
                ExceptionDialog.show(ownerWindow, e);
            } catch (Throwable e1) {
                log.error("Unexpected exception during show error message dialog", e1);
            }
            return false;
        }
    }

    private JButton createImgButton(Icons icon, ActionCode actionCode){
        ImageIcon imageIcon = IconLoader.loadIcon(icon);
        JButton button = new JButton(imageIcon);
        button.setPreferredSize(new Dimension(imageIcon.getIconWidth()+4,
                                              imageIcon.getIconHeight()+4));
        button.setFocusable(false);
        button.setActionCommand(actionCode.name());
        button.addActionListener(this);
        return button;
    }


}